package com.kaifamiao;

import java.lang.reflect.*;
import java.util.Arrays;

public class ProxyClassTest {

    public static void main(String[] args) throws Exception {
        // 代理目标
        final Object target = new Foxconn();
        final Class<?> c = target.getClass();
        final ClassLoader loader = c.getClassLoader();
        final Class<?>[] interfaces = c.getInterfaces();
        InvocationHandler handler = (proxy, method, params) -> method.invoke( target, params ) ;
        // 代理对象
        Object proxy = Proxy.newProxyInstance( loader, interfaces, handler );

        // 获得代理类
        Class<?> proxyClass = proxy.getClass();

        System.out.println( "所在包" );
        System.out.println( proxyClass.getPackageName() );

        System.out.println( "修饰符" );
        System.out.println( Modifier.toString( proxyClass.getModifiers() ) );

        System.out.println( "父类" );
        System.out.println( proxyClass.getSuperclass() );

        System.out.println( "直接实现的接口" );
        System.out.println( Arrays.toString( proxyClass.getInterfaces() ) );

        // 获得代理类中的所有字段
        Field[] fields = proxyClass.getDeclaredFields();
        System.out.println( "字段" );
        for( Field f : fields ){
            System.out.println( f );
        }

        // 获得代理类中的所有构造
        Constructor<?>[] cons = proxyClass.getDeclaredConstructors();
        System.out.println( "构造" );
        for( Constructor con : cons ){
            System.out.println( con );
        }

        // 获得代理类中的所有方法
        Method[] methods = proxyClass.getDeclaredMethods();
        System.out.println( "方法" );
        for( Method m : methods ){
            System.out.println( m );
        }

    }
}
